﻿using Common.Communal;
using ERP.Domain.Processs;
using Sell.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sell.Services.Pro
{
    public class FlowServices : IFlowServices
    {
        public IFlowRepository repository { get; }
        public FlowServices(IFlowRepository _repository)
        {
            repository = _repository;
        }
        /// <summary>
        /// 工艺流程添加
        /// </summary>
        /// <param name="processFlow"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> AddProcessFlow(ProcessFlow processFlow)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "工艺流程添加成功";
            var list = await repository.AddAsync(processFlow);
            res.Data = list;
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 删除工艺流程
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> DeleteProcessFlow(int id)
        {
            ApiResult<int> res = new ApiResult<int>();
            try
            {
                //根据ID获取客户信息
                var pro = await repository.GetModelAsync(id);
                if (pro == null)
                {
                    res.Code = EnumCode.Fail;
                    res.Message = "工艺流程不存在";
                    return res;
                }
                //将IsDelete状态设置为true
                pro.IsDelete = true;
                //更新客户信息
                var result = await repository.Update(pro);
                res.Code = EnumCode.Ok;
                res.Message = "工艺流程删除成功";
                res.Data = result;

            }
            catch (Exception ex)
            {
                res.Code = EnumCode.Fail;
                res.Message = $"删除工艺流程失败:{ex.Message}";
            }
            return await Task.FromResult(res);
        }
        /// <summary>
        /// 工艺流程修改
        /// </summary>
        /// <param name="processFlow"></param>
        /// <returns></returns>
        public async Task<ApiResult<int>> UpdateProcessFlow(ProcessFlow processFlow)
        {
            ApiResult<int> res = new ApiResult<int>();
            res.Code = EnumCode.Ok;
            res.Message = "工艺流程修改成功";
            var list = await repository.Update(processFlow);
            res.Data = list;
            return await Task.FromResult(res);
        }
    }
}
